﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HospitalSystem
{
    public partial class promotioneducation : System.Web.UI.Page
    {
        DataClasses1DataContext db = new DataClasses1DataContext();
        private void MessageBox2(string msg, string urld)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "');" + "window.location.href='" + urld + "';" + "</script>";
            Page.Controls.Add(lbl);
        }
        private List<Activity> GetEvaluationScrerning()
        {
            return (from a in db.Activities
                    join b in db.Services on a.SVCCode equals b.SVCCode
                    where b.SVCDesc.Equals(Label2.Text)
                    select a).ToList();
        }

        protected void Page_Load(object sender, EventArgs e)
        {


            if (Session["CaseNo"] == null)
            {
                MessageBox2("กรุณากดค้นหา และเลือกเด็กที่จะวางแผน", "checkcid3.aspx");
            }

            else
            {
                if (!IsPostBack)
                {

                    //Recieve Valuable from server
                    int index = 0;
                    string CID = (String)Session["CID"];
                    string FirstName = (String)Session["FirstName"];
                    string LastName = (String)Session["LastName"];
                    string CaseNumber = (String)Session["CaseNo"];

                    //Show Value from Valuable(Session)
                    Label3.Text = CaseNumber;
                    Label2.Text = "การส่งเสริมการศึกษา";
                    Label4.Text = FirstName;
                    Label5.Text = LastName;

                    //Bind CheckboxList @GetEvaluationScrerning()
                    CheckBoxList1.DataSource = GetEvaluationScrerning();
                    CheckBoxList1.DataTextField = "ACTDesc";
                    CheckBoxList1.DataValueField = "ACTCode";
                    CheckBoxList1.DataBind();

                    foreach (ListItem l in CheckBoxList1.Items)
                    {
                        var query = from a in db.ServiceProvisionDetails
                                    join b in db.ServiceProvisionVsPersons on a.id equals b.id
                                    where b.CID.Equals(CID) && b.CaseNo.Equals(CaseNumber) && a.ACTCode.Equals(l.Value)
                                    select new
                                    {
                                        a.ACTCode
                                    };
                        if (query.FirstOrDefault() == null)
                        {
                            var queryforDelete = from a in db.Plannigs
                                                 join b in db.Activities on a.SACTCode equals b.ACTCode
                                                 where a.CID.Equals(CID) && a.CaseNo.Equals(CaseNumber) && a.SACTCode.Equals(l.Value)
                                                 select new
                                                 {
                                                     b.SVCCode,
                                                     a
                                                 };
                            if (queryforDelete.FirstOrDefault() == null)
                            {
                                CheckBoxList1.Items[index].Selected = false;
                            }
                            else
                            {
                                CheckBoxList1.Items[index].Selected = true;
                            }
                        }
                        else
                        {
                            CheckBoxList1.Items[index].Selected = true;
                        }
                        index++;
                    }


                }
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            string CaseNumber = (String)Session["CaseNo"];
            string CID = (String)Session["CID"];
            string CMPlanNo = (String)Session["CMPlanNo"];
            string Staff_id = (String)Session["Staff_id"];
            //Delete Data Before New Insert
            var queryforDelete = from r1 in
                                     (from a in db.Plannigs
                                      join b in db.Activities on a.SACTCode equals b.ACTCode
                                      where a.CID.Equals(CID) && a.CaseNo.Equals(CaseNumber)
                                      select new
                                      {
                                          b.SVCCode,
                                          a
                                      })
                                 join c in db.Services on r1.SVCCode equals c.SVCCode
                                 where c.SVCDesc.Equals(Label2.Text)
                                 select r1.a;
            foreach (var q in queryforDelete)
            {
                db.Plannigs.DeleteOnSubmit(q);
                db.SubmitChanges();
            }
            //Insert Data 
            foreach (ListItem l in CheckBoxList1.Items)
            {
                Plannig PL = new Plannig();
                if (l.Selected)
                {
                    PL.SACTCode = l.Value;
                    PL.CaseNo = int.Parse(CaseNumber);
                    PL.CID = CID;
                    PL.CMPlanNo = "1";
                    PL.ExpectedDate = DateTime.Now;
                    PL.Staff_id = Staff_id;
                    db.Plannigs.InsertOnSubmit(PL);
                    db.SubmitChanges();
                }
            }


            Response.Redirect("healthpromotion.aspx");
        }


    }
}